﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PotentialRentals.DAL;
using PotentialRentals.Models;

namespace PotentialRentals.Repositories
{
    public class UnitOfWork : IUnitOfWork
    {
        #region properties

        private RentalDBContext _db;
        private RentalRepository _RentalRepo;

        public IEnumerable<RentType> RentalTypes
        {
            get
            {
                return _db.RentTypes;
            }
        }

        public IEnumerable<string> BedroomList
        {
            get
            {
                var bedrooms = new[] { 
                    "0", "1", "2", "3", "4", "5" 
                };

                return bedrooms;
            }
        }

        public IEnumerable<string> BathroomList
        {
            get
            {
                var bathrooms = new[] { 
                    "0", "1", "1.5", "2", "2.5", "3", "3.5", "4" 
                };

                return bathrooms;
            }
        }

        public IEnumerable<RentalAmenity> RentalAmenityList
        {
            get
            {
                return _db.RentalAmenities;
            }
        }

        public IEnumerable<AddressAmenity> AddressAmenityList
        {
            get
            {
                return _db.AddressAmenities;
            }
        }

        public IEnumerable<State> StateList
        {
            get
            {
                return _db.States;
            }
        }

        #endregion

        public UnitOfWork(RentalDBContext db)
        {
            _db = db ?? new RentalDBContext();
        }

        public User GetUser(string userName)
        {
            var user = _db.Users.SingleOrDefault(x => x.UserName == userName);
            if (user == null)
            {
                user = new User();
                user.UserName = userName != "" ? userName : "sessionUser";
                _db.Users.Add(user);
            }

            return user;
        }

        public RentalRepository RentalRepo
        {
            get
            {
                if (_RentalRepo == null)
                {
                    _RentalRepo = new RentalRepository(_db);
                }
                return _RentalRepo;
            }
        }

        public void Save()
        {
            _db.SaveChanges();
        }

        public void Dispose()
        {
            _db.Dispose();
        }
    }
}